package com.hyfrogx.modules.app.controller;

import cn.hutool.core.bean.BeanUtil;
import com.hyfrogx.common.param.Constants;
import com.hyfrogx.common.qcloud.UploadUtils;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.modules.app.form.FeedbackForm;
import com.hyfrogx.modules.manage.entity.feedback.FeedbackEntity;
import com.hyfrogx.modules.manage.service.feedback.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.Iterator;

/**
 * @ClassName: ApiFeedbackController
 * @Description: TODO 反馈控制类
 * @author Li
 * @date 2018年6月11日 上午11:46:48
 *
 */
@RestController
@RequestMapping("api/feedback")
public class ApiFeedbackController{

	@Autowired
	FeedbackService feedbackService;

	/*
	 * 用户反馈
	 */
	@PostMapping(value = "/feedbackput")
	public R userFeedback(@Valid FeedbackForm form, BindingResult result, HttpServletRequest request) {
		try {
			if (result.hasErrors()) {
				return R.error(-1, result.getAllErrors().get(0).getDefaultMessage());
			}

			FeedbackEntity feedback=new FeedbackEntity();
			BeanUtil.copyProperties(form,feedback);

			CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
					request.getSession().getServletContext());
			if (multipartResolver.isMultipart(request)) {
				MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
				Iterator<String> iter = multiRequest.getFileNames();
				while (iter.hasNext()) {
					MultipartFile file = multiRequest.getFile(iter.next());
					if (file != null) {
						String url = UploadUtils.uploadWebFile(file,Constants.Path.FEEDBACK_put);
						if (file.getName().equals("img_1")) {
							feedback.setImga(url);
						}else if (file.getName().equals("img_2")) {
							feedback.setImgb(url);
						}else if (file.getName().equals("img_3")) {
							feedback.setImgc(url);
						}else if (file.getName().equals("img_4")) {
							feedback.setImgd(url);
						}else if (file.getName().equals("img_5")) {
							feedback.setImge(url);
						}else if (file.getName().equals("img_6")) {
							feedback.setImgf(url);
						}
					}
				}
			}
			feedback.setCreatedate(new Date());
			feedbackService.getBaseMapper().insert(feedback);
			return R.ok("反馈成功");
		} catch (Exception e) {
			return R.error(e.getMessage());
		}
	}

}
